﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        //从命令行参数或控制台输入获取文件路径，支持Drag & Drop操作
        var filePath = string.Empty;
        if (args.Length == 0)
        {
            Console.WriteLine("Please provide a file path.");
            filePath = Console.ReadLine();
        }
        else
        {
            filePath = args[0];
        }

        // 读取 Excel 文件并存储数据到列表中
        var testList = ReadExcel(filePath);
        while (true)
        {
            Console.WriteLine("请输入要查询的 Name（输入 'exit' 退出）：");
            var inputName = Console.ReadLine();
            if (inputName.ToLower() == "exit")
            {
                break;
            }
            var foundTest = testList.FirstOrDefault(t => t.Name == inputName);
            Console.WriteLine(foundTest != null
                ? $"Name: {foundTest.Name}, Layer: {foundTest.Layer}, ParamSize: {foundTest.ParamSize}"
                : $"未找到名为 {inputName} 的记录。");
        }
    }

    private static List<Test> ReadExcel(string filePath)
    {
        var testList = new List<Test>();

        try
        {
            using var spreadsheetDocument = SpreadsheetDocument.Open(filePath, false);
            var workbookPart = spreadsheetDocument.WorkbookPart;
            var worksheetPart = workbookPart.WorksheetParts.FirstOrDefault();

            if (worksheetPart != null)
            {
                var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
                var isHeader = true;
                foreach (var row in sheetData.Elements<Row>())
                {
                    if (isHeader)
                    {
                        isHeader = false;
                        continue; // 跳过表头行
                    }

                    var rowValues = row.Elements<Cell>().Select(cell => GetCellValue(workbookPart, cell)).ToList();

                    if (rowValues.Count < 3) continue;
                    var test = new Test
                    {
                        Layer = rowValues[0],
                        Name = rowValues[1],
                        ParamSize = int.TryParse(rowValues[2], out var paramSize) ? paramSize : 0
                    };
                    testList.Add(test);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"读取文件时发生错误：{ex.Message}");
        }

        return testList;
    }

    private static string GetCellValue(WorkbookPart workbookPart, Cell cell)
    {
        if (cell.CellValue == null)
        {
            return string.Empty;
        }

        var value = cell.CellValue.InnerText;

        if (cell.DataType == null || cell.DataType.Value != CellValues.SharedString) return value;
        var stringTablePart = workbookPart.SharedStringTablePart;
        if (stringTablePart != null)
        {
            value = stringTablePart.SharedStringTable.ChildElements[int.Parse(value)].InnerText;
        }

        return value;
    }
}

internal class Test
{
    public string Name { get; set; }
    public string Layer { get; set; }
    public int ParamSize { get; set; }
}